/** 
 * @ProjectName:CYFrameAndroid  
 * @Title: DensityUtils.java 
 * @Package com.cyou.cyframeandroid.util 
 * @Description: (用一句话描述该文件做什么) 
 * @author liuqi qiliu_17173@cyou-inc.com   
 * @date 2013-8-19 下午2:37:32 
 * @version V1.0   
 * Copyright (c) 2013搜狐公司-版权所有
 */
package com.common.core.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.common.core.constants.CommonConstants;

/**
 * @ClassName: DensityUtils
 * @Description: (方便进行px和dp之间的转换)
 * @date 2013-8-19 下午2:37:32
 */
public class DensityUtils {
	/**
	 * 
	 * @Title: dip2px
	 * @Description: (根据手机的分辨率从 dp 的单位 转成为 px(像素))
	 * @param: @param context
	 * @param: @param dpValue
	 * @param: @return 设定文件
	 * @return: int 返回类型
	 * @date: 2013-8-19 下午2:38:08
	 */
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	/**
	 * 
	 * @Title: px2dip
	 * @Description: (根据手机的分辨率从 px(像素) 的单位 转成为 dp )
	 * @param: @param context
	 * @param: @param pxValue
	 * @param: @return 设定文件
	 * @return: int 返回类型
	 * @date: 2013-8-19 下午2:38:24
	 */
	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}
	
	/** 
     * 将px值转换为sp值，保证文字大小不变 
     *  
     * @param pxValue 
     *            （DisplayMetrics类中属性scaledDensity）
     * @return 
     */  
    public static int px2sp(Context context, float pxValue) {  
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;  
        return (int) (pxValue / fontScale + 0.5f);  
    }  
  
    /** 
     * 将sp值转换为px值，保证文字大小不变 
     *  
     * @param spValue 
     *            （DisplayMetrics类中属性scaledDensity）
     * @return 
     */  
    public static int sp2px(Context context, float spValue) {  
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;  
        return (int) (spValue * fontScale + 0.5f);  
    }


	/**
	 * 将一倍尺寸缩放到当前屏幕大小的尺寸（宽）
	 */
	public static int zoomWidth(int w) {
		int sw = 0;
		sw = Math.min(getScreenWidth(), getScreenHeight());

		return Math.round(w * sw / 320f + 0.5f);
	}

	/**
	 * 将一倍尺寸缩放到当前屏幕大小的尺寸（高）
	 */
	public static int zoomHeight(int h) {
		int sh = 0;
		sh = getScreenHeight();

		return (int) (h * sh / 480f + 0.5f);
	}

	/**
	 * 缩放控件
	 */
	public static void zoomView(int w, int h, View view) {
		if (view == null) {
			return;
		}

		ViewGroup.LayoutParams params = view.getLayoutParams();

		if (params == null) {
			return;
		}

		params.width = zoomWidth(w);
		params.height = zoomWidth(h);
	}

	/**
	 * 缩放控件
	 */
	public static void zoomViewHeight(int h, View view) {
		if (view == null) {
			return;
		}

		ViewGroup.LayoutParams params = view.getLayoutParams();

		if (params == null) {
			return;
		}

		params.height = zoomWidth(h);
	}

	/**
	 * 缩放控件
	 */
	public static void zoomViewWidth(int w, View view) {
		if (view == null) {
			return;
		}

		ViewGroup.LayoutParams params = view.getLayoutParams();

		if (params == null) {
			return;
		}

		params.width = zoomWidth(w);
	}

	/**
	 * 缩放控件
	 */
	public static void zoomViewFull(View view) {
		if (view == null) {
			return;
		}

		ViewGroup.LayoutParams params = view.getLayoutParams();

		if (params == null) {
			return;
		}

		params.width = getScreenWidth();
		params.height = getScreenHeight();
	}

	/**
	 * dip转px
	 */
	public static int dipToPx(int dipValue) {
		final float scale = CommonConstants.displayMetrics.density;
		int pxValue = (int) (dipValue * scale + 0.5f);

		return pxValue;
	}
	public static int px2sp(float pxValue) {
		final float fontScale = CommonConstants.displayMetrics.scaledDensity;
		return (int) (pxValue / fontScale + 0.5f);
	}
	/**
	 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
	 */
	public static int px2dip(float pxValue) {
		final float scale = CommonConstants.displayMetrics.density;
		return (int) (pxValue / scale + 0.5f);
	}

	/**
	 * px转dip
	 */
	public static float dipToPxFloat(int dipValue) {
		final float scale = CommonConstants.displayMetrics.density;
		float pxValue = dipValue * scale;

		return pxValue;
	}

	/**
	 * dip转px
	 */
	public static int dipToPx(Context context, int dipValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		int pxValue = (int) (dipValue * scale + 0.5f);

		return pxValue;
	}


	/**
	 * 获取密度
	 *
	 * @return
	 */
	public static String getDensity() {
		DisplayMetrics dm = ContextProvider.getApplicationContext().getResources().getDisplayMetrics();
		return String.valueOf(dm.density);
	}

	/**
	 * 得到屏幕宽度
	 */    public static int getScreenWidth() {
		return ((WindowManager) ContextProvider.getApplicationContext().getSystemService(Context.WINDOW_SERVICE))
				.getDefaultDisplay().getWidth();
	}

	/**
	 * 得到屏幕高度
	 */
	public static int getScreenHeight() {
		return ((WindowManager) ContextProvider.getApplicationContext().getSystemService(Context.WINDOW_SERVICE))
				.getDefaultDisplay().getHeight();
	}

	/**
	 * 获取分辨率
	 *
	 * @return
	 */
	public static String getResolution() {
		DisplayMetrics dm = ContextProvider.getApplicationContext().getResources().getDisplayMetrics();
		return new StringBuilder().append(dm.widthPixels).append("*").append(dm.heightPixels).toString();
	}
	/**
	 * 设置为横屏
	 */
	public static void screenLandscape(Activity activity) {
		activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	}

	/**
	 * 设置为竖屏
	 */
	public static void screenPortrait(Activity activity) {
		activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}
	/**
	 * 根据资源ID得到View
	 */
	public static View inflate(Context context, int resource, ViewGroup root, boolean attachToRoot) {
		return LayoutInflater.from(context).inflate(resource, root, attachToRoot);
	}

	/**
	 * 根据资源ID得到View
	 */
	public static View inflate(Context context, int resource, ViewGroup root) {
		return LayoutInflater.from(context).inflate(resource, root);
	}

	/**
	 * 根据资源ID得到View
	 */
	public static View inflate(LayoutInflater inflater, int resource, ViewGroup root, boolean attachToRoot) {
		return inflater.inflate(resource, root, attachToRoot);
	}
}
